home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MEMORY.SWG / 0008_MALLOC.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  3KB  |  70 lines

  1. {$S-,R-,V-,I-,N-,B-,F-}
  2.  
  3. {$IFNDEF Ver40}
  4. {Allow overlays}
  5. {$F+,O-,X+,A-}
  6. {$ENDIF}
  7.  
  8. UNIT MemAlloc;
  9.  
  10.   { Purpose is to provide the ability to create (destroy) dynamic variables  }
  11.   { without needing to reserve heap space at compile time.                   }
  12.  
  13. INTERFACE
  14.  
  15. FUNCTION Malloc(VAR Ptr; Size : Word) : Word;
  16.   { Allocate free memory and return a pointer to it.  The amount of memory      }
  17.   { requested from DOS is calculated as (Size/4)+1 paragraphs.  If the          }
  18.   { allocation is successful, the untyped VAR parameter Ptr will be populated   }
  19.   { with the address of the allocated memory block, and the function will return}
  20.   { a zero result.  Should the request to DOS fail, Ptr will be populated with  }
  21.   { the value NIL, and the function will return the appropriate DOS error code. }
  22.  
  23. FUNCTION Dalloc(VAR Ptr) : Word;
  24.   { Deallocate the memory pointed to by the untyped VAR parameter Ptr           }
  25.  
  26. IMPLEMENTATION
  27.  
  28.   FUNCTION Malloc(VAR Ptr; Size : Word) : Word;
  29.   BEGIN
  30.     INLINE(
  31.       $8B / $46 / <Size /         {            mov         ax,[bp+<Size]}
  32.       $B9 / $04 / $00 /           {            mov         cx,4}
  33.       $D3 / $E8 /                 {            shr         ax,cl}
  34.       $40 /                       {            inc         ax}
  35.       $89 / $C3 /                 {            mov         bx,ax}
  36.       $B4 / $48 /                 {            mov         ah,$48}
  37.       $CD / $21 /                 {            int         $21             ;Allocate memory}
  38.       $72 / $07 /                 {            jc          AllocErr        ;If any errors ....}
  39.       $C7 / $46 / $FE / $00 / $00 / {NoErrors:   mov word    [bp-2],0        ;Return 0 for successful allocation}
  40.       $EB / $05 /                 {            jmp short   Exit}
  41.       $89 / $46 / $FE /           {AllocErr:   mov         [bp-2],ax       ;Return error code}
  42.       $31 / $C0 /                 {            xor         ax,ax           ;Store a NIL value into the ptr}
  43.       $C4 / $7E / <Ptr /          {Exit:       les         di,[bp+<Ptr]    ;Address of pointer into es:di}
  44.       $50 /                       {            push        ax              ;Save the Segment part}
  45.       $31 / $C0 /                 {            xor         ax,ax           ;Offset is always 0}
  46.       $FC /                       {            cld                         ;Make sure direction is upward}
  47.       $AB /                       {            stosw                       ;Store offset of memory block}
  48.       $58 /                       {            pop         ax              ;Get back segment part}
  49.       $AB);                       {            stosw                       ;Store segment of memory block}
  50.     
  51.   END {Malloc} ;
  52.  
  53.   FUNCTION Dalloc(VAR Ptr) : Word;
  54.   BEGIN
  55.     IF Pointer(Ptr) <> NIL THEN BEGIN
  56.       INLINE(
  57.         $B4 / $49 /               {            mov         ah,$49}
  58.         $C4 / $7E / <Ptr /        {            les         di,[bp+<Ptr]}
  59.         $26 / $C4 / $3D /         {        es: les         di,[di]}
  60.         $CD / $21 /               {            int         $21}
  61.         $72 / $02 /               {            jc          Exit}
  62.         $31 / $C0 /               {NoError:    xor         ax,ax}
  63.         $89 / $46 / $FE);         {Exit:       mov         [bp-2],ax}
  64.       Pointer(Ptr) := NIL;
  65.     END {if} ;
  66.   END {Dealloc} ;
  67.  
  68. END {Unit MemAlloc} .
  69.  
  70.